package com.geovis.emergency.spd.biz.basic.wrapper;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.geovis.emergency.spd.biz.pc.wrapper.BaseEntityWrapper;
import com.geovis.emergency.spd.entity.basic.entity.BasicLabel;
import com.geovis.emergency.spd.entity.basic.pojo.vo.BasicLabelVO;
import com.geovis.emergency.spd.entity.basic.pojo.vo.LabelVO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.log4j.Log4j2;

import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @author Lonsho
 * @version 1.0
 * @date 2022/3/16
 */

@Log4j2
public class BasicLabelWrapper extends BaseEntityWrapper<BasicLabel, BasicLabelVO> {
    public static BasicLabelWrapper build(){
        return new BasicLabelWrapper();
    }
    @Override
    public BasicLabelVO entityVO(BasicLabel entity) {
        return null;
    }
    public List<LabelVO> labelVO(List<BasicLabel> cfgLabelList){
        List<LabelVO> resultList = Lists.newArrayList();
        Map<String, List<BasicLabel>> map = Maps.newHashMap();
        for (BasicLabel cfgLabel : cfgLabelList) {
            //首字母
            String firstChar = String.valueOf(PinyinUtil.getFirstLetter(cfgLabel.getName(), "").charAt(0)).toUpperCase(Locale.ROOT);
            List<BasicLabel> list = map.getOrDefault(firstChar, Lists.newArrayList());
            list.add(cfgLabel);
            map.put(firstChar,list);
        }
        List<LabelVO> otherResult = Lists.newArrayList();
        for (Map.Entry<String, List<BasicLabel>> entry : map.entrySet()) {
            if(NumberUtil.isNumber(entry.getKey())){
                otherResult.add(new LabelVO("#",entry.getValue()));
            }else {
                resultList.add(new LabelVO(entry.getKey(),entry.getValue()));
            }
        }
        resultList.sort(Comparator.comparing(LabelVO::getGroupChar));
        resultList.addAll(otherResult);
        return resultList;
    }
}
